#! /bin/bash
JOBNUM=4
NPWD=`realpath .`
CODETOP=`realpath ../../`
export ARCH=arm
export CROSS_COMPILE=$CODETOP/gcc/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
CONFIG_BOARD="OPIONE"
#CONFIG_BOARD="BBB"


mkdir -p images
cd images
mkdir -p bootfile
mkdir -p bootdir
mkdir -p linuxdir
echo $PWD
SDIMG="sd.img"
BOOTPARTSIZE=10
FSPARTSIZE=10
if [ $BOOTPARTSIZE -lt 10 ];then
        echo "boot part size must larget than 10M"
        exit 1
fi
if [ $FSPARTSIZE -lt 10 ];then
        echo "rootfs part size must larget than 10M"
        exit 1
fi

#boot partion
dd if=/dev/zero of=${SDIMG}1 bs=1M count=$BOOTPARTSIZE > /dev/null 2>&1
echo "Formating fat partition for boot..."
sudo mkfs -t vfat -F 32 -n BOOT ${SDIMG}1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
        echo "ERROR formating fat boot partition."
        exit 0
fi
vfatuuid=`sudo blkid -s UUID -o value ${SDIMG}1`
echo "  fat boot partition formated."
if ! sudo mount ${SDIMG}1 bootdir; then
        echo "ERROR mounting fat boot partitions..."
        exit 1
fi
if [ "${CONFIG_BOARD}_x" == "BBB_x" ];then
	sudo cp -rf $NPWD/ubootbuild/MLO $NPWD/images/bootdir/
	sudo cp -rf $NPWD/ubootbuild/u-boot.img $NPWD/images/bootdir/
	sudo cp -rf $NPWD/linuxbuild/arch/arm/boot/zImage $NPWD/images/bootdir/
	sudo cp -rf $NPWD/linuxbuild/arch/arm/boot/dts/am335x-boneblack.dtb $NPWD/images/bootdir/
	sudo cp -rf $NPWD/ubootbuild/MLO $NPWD/images/bootfile/
	sudo cp -rf $NPWD/ubootbuild/u-boot.img $NPWD/images/bootfile/
elif [ "${CONFIG_BOARD}_x" == "OPIONE_x" ];then
	sudo cp -rf $NPWD/linuxbuild/arch/arm/boot/zImage $NPWD/images/bootdir/
	sudo cp -rf $NPWD/linuxbuild/arch/arm/boot/dts/sun8i-h3-orangepi-one.dtb $NPWD/images/bootdir/
elif [ "${CONFIG_BOARD}_x" == "OPIWIN_x" ];then
	sudo cp -rf $NPWD/linuxbuild/arch/arm64/boot/Image $NPWD/images/bootdir/
	sudo cp -rf $NPWD/linuxbuild/arch/arm64/boot/dts/allwinner/sun50i-a64-orangepi-win.dtb $NPWD/images/bootdir/
fi
sudo cp -rf $NPWD/env.txt $NPWD/images/bootdir/
sync
if ! sudo umount bootdir; then
        echo "ERROR unmounting fat boot partition."
fi

#rootfs partion
dd if=/dev/zero of=${SDIMG}2 bs=1M count=$FSPARTSIZE > /dev/null 2>&1
echo "Formating rootfs linux partition ..."
sudo mkfs -F -t ext4 -L ROOTFS ${SDIMG}2 > /dev/null 2>&1
#sudo mkfs -F -t ext4 -b 4096 -E stride=2,stripe-width=1024  -L ROOTFS ${SDIMG}2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
        echo "ERROR formating rootfs linux partition."
        exit 0
fi
vfatuuid=`sudo blkid -s UUID -o value ${SDIMG}2`
echo "  rootfs linux partition formated."
if ! sudo mount ${SDIMG}2 linuxdir; then
        echo "ERROR mounting rootfs linux partitions..."
        exit 1
fi
sudo rm -rf $NPWD/rootfs/root/*
sudo cp -rf $NPWD/env.txt $NPWD/rootfs/root
sudo cp -rfa $NPWD/rootfs/* linuxdir > /dev/null 2>&1
#sudo rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats $NPWD/rootfs/ linuxdir > /dev/null 2>&1
sync
if ! sudo umount linuxdir; then
        echo "ERROR unmounting rootfs linux partitions."
fi



#create img to burn
TABLESIZE=2048
FATSIZE=+${BOOTPARTSIZE}M
EXTSIZE=+${FSPARTSIZE}M
TOTALSIZE=$(expr ${BOOTPARTSIZE} + ${FSPARTSIZE} + 2)
dd if=/dev/zero of=${SDIMG} bs=1M count=$TOTALSIZE > /dev/null 2>&1


#cp SPL and bootloade to sd
echo "create partition table"
if [ "${CONFIG_BOARD}_x" == "BBB_x" ];then
#beaglebonblack first part
	dd if=$NPWD/images/bootfile/MLO of=${SDIMG} count=1 seek=1 conv=notrunc bs=128k > /dev/null 2>&1
	dd if=$NPWD/images/bootfile/u-boot.img of=${SDIMG} count=2 seek=1 conv=notrunc bs=384k > /dev/null 2>&1

elif [ "${CONFIG_BOARD}_x" == "OPIONE_x" ];then
#orangepi
	#dd if=$NPWD/ubootbuild/u-boot-sunxi-with-spl.bin of=${SDIMG} seek=1 conv=notrunc bs=8k > /dev/null 2>&1
	#CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR=0x50 so x050*512/1024=40k
	dd if=$NPWD/ubootbuild/spl/sunxi-spl.bin of=${SDIMG} seek=8 conv=notrunc bs=1k  > /dev/null 2>&1
	dd if=$NPWD/ubootbuild/u-boot-dtb.bin of=${SDIMG} seek=1 conv=notrunc bs=40k > /dev/null 2>&1
elif [ "${CONFIG_BOARD}_x" == "OPIWIN_x" ];then
	#orangepi win
	#dd if=$NPWD/ubootbuild/spl/sunxi-spl.bin of=${SDIMG} seek=1 count=4 conv=notrunc bs=8k > /dev/null 2>&1
	#dd if=$NPWD/ubootbuild/u-boot.itb of=${SDIMG} seek=1 count=20 conv=notrunc bs=40k > /dev/null 2>&1
	dd if=$NPWD/ubootbuild/u-boot-sunxi-with-spl.bin of=${SDIMG} seek=1 conv=notrunc bs=8k  > /dev/null 2>&1
fi

#create img partition table
echo -e "n\np\n1\n$TABLESIZE\n$FATSIZE\nn\np\n2\n \n$EXTSIZE\nt\n1\nc\na\n2\nw\n" | sudo fdisk ${SDIMG} > /dev/null 2>&1
sync
sleep 2
partprobe -s ${SDIMG}  > /dev/null 2>&1
if [ $? -ne 0 ]; then
        echo "make img partition table error"
        exit 1
fi
dd if=${SDIMG} of=${SDIMG}t bs=512 count=$TABLESIZE > /dev/null 2>&1
rm -rf ${SDIMG}
echo "  create partition table success"

#merge imgt and img1 and img2 to img
echo "mergeing image "
if [ ! -f ${SDIMG}t ]; then
        echo "error there is not ${SDIMG}t"
        exit 1
fi
if [ ! -f ${SDIMG}1 ]; then
        echo "error there is not ${SDIMG}1"
        exit 1
fi
if [ ! -f ${SDIMG}2 ]; then
        echo "error there is not ${SDIMG}2"
        exit 1
fi
dd if=${SDIMG}t of=${SDIMG} bs=512 count=$TABLESIZE  > /dev/null 2>&1
dd if=${SDIMG}1 of=${SDIMG} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
dd if=${SDIMG}2 of=${SDIMG} bs=1M conv=notrunc oflag=append > /dev/null 2>&1
sync
sleep 2
partprobe -s ${SDIMG}
if [ $? -ne 0 ]; then
        echo "make img partition table error"
        exit 1
fi
echo "create successful"
